package com.yfqy.admin.resource;

import com.github.pagehelper.PageInfo;
import com.yfqy.admin.constant.MyConstants;
import com.yfqy.admin.domain.dto.QueryById;
import com.yfqy.admin.domain.dto.product.ProductQuery;
import com.yfqy.admin.domain.dto.product.ProductSkuDTO;
import com.yfqy.admin.domain.dto.product.ProductSpuDTO;
import com.yfqy.admin.domain.dto.product.UpAndDownProduct;
import com.yfqy.admin.service.ProductService;
import com.yfqy.admin.domain.vo.common.ResultVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/internal/product")
public class ProductController {

    public final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 详情接口
     *
     * @param queryById
     * @return
     */
    @PostMapping("/detail")
    public ResultVO<ProductSpuDTO> detail(@RequestBody @Validated QueryById queryById) {
        return ResultVO.data(productService.detail(queryById.getId()));
    }

    /**
     * 分页查询
     *
     * @param productQuery
     * @return
     */
    @PostMapping("/pageList")
    public ResultVO<PageInfo<ProductSpuDTO>> pageList(@RequestBody ProductQuery productQuery) {
        return ResultVO.data(productService.pageList(productQuery));
    }

    /**
     * 新增商品
     *
     * @param addProduct
     * @return
     */
    @PostMapping("/add")
    public ResultVO<String> addProduct(@RequestBody @Validated ProductSpuDTO addProduct) {
        int insert = productService.addProduct(addProduct);
        return insert > 0 ? ResultVO.success("新增商品成功") : ResultVO.fail("新增商品失败");
    }

    /**
     * 编辑商品
     *
     * @param modifyProduct
     * @return
     */
    @PostMapping("/modify")
    public ResultVO<String> modifyProduct(@RequestBody @Validated ProductSpuDTO modifyProduct) {
        int insert = productService.modifyProduct(modifyProduct);
        return insert > 0 ? ResultVO.success("编辑商品成功") : ResultVO.fail("编辑商品失败");
    }

    /**
     * 商品上下架
     *
     * @param upAndDownProduct
     * @return
     */
    @PostMapping("/upAndDown")
    public ResultVO<String> upAndDown(@RequestBody @Validated UpAndDownProduct upAndDownProduct) {
        int upAndDown = productService.upAndDown(upAndDownProduct);
        String message = upAndDownProduct.getStatus() == MyConstants.BT1 ? "上架" : "下架";
        return upAndDown > 0 ? ResultVO.success(message + "成功") : ResultVO.fail(message + "失败");
    }

    @GetMapping("/skuList")
    public ResultVO<List<ProductSkuDTO>> skuList(@RequestParam("spuId") Long spuId) {
        return ResultVO.data(productService.skuList(spuId));
    }
}
